package com.pengtu.gsj.service.impl.system;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.ibm.icu.util.Calendar;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.dao.system.CodeDao;
import com.pengtu.gsj.dao.system.OrganizationDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.app.Code;
import com.pengtu.gsj.entity.app.Organization;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.security.SpringSecurityUtils;
import com.pengtu.gsj.service.system.OrganizationService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.Constants;
import com.pengtu.gsj.utils.ConverPdfUtils;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.ExcelImportUtils;
import com.pengtu.gsj.utils.FileUtils;
import com.pengtu.gsj.utils.PropertiesUtils;
import com.pengtu.gsj.utils.UpDownFileUtils;
import com.pengtu.gsj.utils.UserUtils;

/**
 * @author zl
 * @version 创建时间：2017年4月5日 上午9:50:52 类说明
 */
@Service
@Transactional
public class SystemManagerServiceImpl implements SystemManageService {

	@Autowired
	private CodeDao codeDao;
	
	@Autowired
	private AttachmentDao attachmentDao;
	
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	

	// -- 码表管理 Manager --//
	@Override
	public Code getCode(String id) {
		return codeDao.get(id);
	}

	@Override
	public String getFullPath(String partId) {
		List<Attachment> list = attachmentDao
				.getAttachmentsByPartId(partId);
		String upfilePath = null;
		Properties props;
		try {
			props = PropertiesUtils.loadProperties("config.properties");
			upfilePath = props.getProperty("file.path");
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (list != null && list.size() > 0) {
			Attachment attachment = list.get(0);
			return upfilePath + attachment.getFullPath();
		}
		return upfilePath;
	}

	@Override
	public String getSwfFullPath(String partId) {
		List<Attachment> list =  attachmentDao.getAttachmentsByPartId(partId);
		String upfilePath = null;
		String path =null;
		Properties props;
		try {
			props = PropertiesUtils.loadProperties("config.properties");
			upfilePath = props.getProperty("file.path");
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(list != null&&list.size()>0){
			Attachment attachment = list.get(0);
			path = upfilePath + attachment.getFullPath();
			return path.substring(0, path.lastIndexOf(".")+1)+"swf";
		}
		return upfilePath;
	}

	@Override
	public String getFullPathById(String id) {
		Attachment attachment=  attachmentDao.get(id);
		String upfilePath = null;
		Properties props;
		try {
			props = PropertiesUtils.loadProperties("config.properties");
			upfilePath = props.getProperty("file.path");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return upfilePath + attachment.getFullPath();
	}

	@Transactional(readOnly = true)
	@Override
	public List<Code> getCodeList() {
		return codeDao.getAll();
	}

	/**
	 * 
	 * getCodeList:
	 * 适用:所有的码表列表
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	@Override
	public Page<Code> findCodes(Page<Code> page, QueryFilter queryFilter) {
		return codeDao.findPage(page, queryFilter);
	}

	@Override
	public List<Code> findCodes(QueryFilter queryFilter) {
		return codeDao.find(queryFilter);
	}
	
	/**
	 * 未标识弃用的码表列表
	 * getAvailableCodeList:
	 * 适用:
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	@Override
	public List<Code> getAvailableCodeList() {
		return codeDao.findBy("useFlag", Constants.ACTION, "category,codeId", "asc,asc");
	}

	
	/**
	 * 
	 * 根据种类查询改种类码表下所有码表
	 * 适用: 
	 * @param category
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	@Override
	public List<Code> getCodeListByCategory(Integer category) {
		return codeDao.findBy("category", category,"codeId",true);
	}

	/**
	 * 
	 * getCodeByCategory:
	 * 根据category 与 codeId 查询对应的Code
	 * @param category
	 * @param codeId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	@Override
	public Code getCodeByCategory(Integer category, String codeId) {
		return codeDao.getCodeByCategory(category, codeId);
	}

	@Override
	public Code getCodeNameByCategory(Integer category, String codeName) {
		return codeDao.getCodeIdByCategory(category, codeName);
	}

	@Override
	public void deleteCode(String id) {
		codeDao.delete(id);
	}

	@Override
	public void deleteCodeAll() {
		codeDao.deleteAll();
	}

	@Transactional(readOnly = true)
	@Override
	public Attachment getAttachment(String id) {
		return attachmentDao.get(id);
	}

	@Override
	public void saveAttachment(Attachment attachment) {
		attachmentDao.save(attachment);
	}

	@Override
	public boolean deleteAttachment(String rootPath, String id) {
		Attachment attachment = getAttachment(id);
		attachmentDao.delete(attachment);
		return true;
	}

	@Transactional(readOnly = true)
	@Override
	public List<Attachment> getAttachmentList(QueryFilter fileter) {
		return attachmentDao.find(fileter, "createDate", true);
	}

	@Override
	public Attachment uploadFile(String rootPath, Attachment attachment,
			String tableId, String tableName, String tableField) throws Exception {
		 attachment.setTableId(tableId);
		 attachment.setTableName(tableName);
		 attachment.setTableField(tableField);
		return uploadFile(rootPath,attachment);
	}

	@Override
	public Attachment uploadFile(String rootPath, Attachment attachment) throws Exception {
		// 不是修改附件信息
   	 if(StringUtils.isEmpty(attachment.getId())){
			//保存到服务器端
   		if(attachment.getUpfile() != null){
				UpDownFileUtils.processUploadedFile(rootPath,attachment);
   		}
			// 上传用户
			attachment.setUploadPerson(SpringSecurityUtils.getCurrentUserName());
   	 }
   	 saveAttachment(attachment);
		return attachment;
	}

	@Transactional(readOnly=true)
	@Override
	public Page<Attachment> getAttachmentList(Page<Attachment> page,
			QueryFilter filters) {
		return attachmentDao.findPage(page, filters);
	}

	@Transactional(readOnly=true)
	@Override
	public List<Attachment> getAttachmentList(String tableId, String tableField) {
		return attachmentDao.getAttachmentList(tableId, tableField);
	}

	@Override
	public List<Attachment> getAttachmentList(String tableId) {
		return attachmentDao.getAttachmentList(tableId);
	}
	
	/**
	 * 
	 * getAll:
	 * 适用:查询所有的attachment不带任何参数的
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly=true)
	@Override
	public List<Attachment> getAllAttachment() {
		return attachmentDao.getAll();
	}

	@Override
	public List<Attachment> getAttachmentBy(String propertyName, String value) {
		return attachmentDao.findBy(propertyName, value);
	}

	@Override
	public void delete(String tableId, String tableField) {
		List<Attachment> atts = attachmentDao.getAttachmentList(tableId, tableField);
		for(Attachment t:atts){
			attachmentDao.delete(t);
		}
	}

	@Override
	public int updateAttachmentTableId(String oldId, String newId) {
		return attachmentDao.updateTableId(oldId, newId);
	}

	@Override
	public String getExistAttachment(String tableId, String tableField) {
		List<Attachment> atts = attachmentDao.getAttachmentList(tableId, tableField);
		if(atts.size()>0){
			return "有";
		}else{
			return "无";
		}
	}

	@Override
	public List<Attachment> findAttachmensByTableId(String tableId) {
		return attachmentDao.getAttachmentList(tableId);
	}

	@Override
	public List<Attachment> findAttachmentsByTableName(String tableName) {
		return attachmentDao.getAttachmentsByTabelName(tableName);
	}

	@Override
	public List<Attachment> findAttachmentsByPartId(String partId) {
		return attachmentDao.getAttachmentsByPartId(partId);
	}

	@Override
	public List<Organization> getAllJu() {
		return organizationDao.getAllJu();
	}

	@Override
	public boolean  deleteAttachment(Attachment attachment) {
		 //去掉关联关系
		 attachment.setPartId("");
		 attachmentDao.delete(attachment);
		 return true;
	}

	@Override
	public List<Attachment> getAttachmentListByTableId(String tableId,
			String partId) {
		if (StringUtils.isEmpty(tableId) || StringUtils.isEmpty(partId)) {
			return null;
		}
		return attachmentDao.getAttachmentListByTableId(tableId, partId);
	}

	@Override
	public List<Attachment> getCheckAttachments(CheckProject checkProject, String tag) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(checkProject.getStartTime()+"-01-01"), MatchType.GE);
		queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(checkProject.getEndTime()+"-12-31"), MatchType.LE);
		queryFilter.addFilter("partId", "", MatchType.NULL);
		return getAttachmentList(queryFilter);
	}
	
	/**
	 * 
	 * getCheckAttachments:
	 * 适用: 根据国省检的起点、结束时间、附件标签查询相关的规章制度和其他类型文件
	 * @param startTime 开始年份时间 eg：2013
	 * @param endTime 结束年份时间  eg：2017
	 * @param tag 文件标签
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getCheckAttachments(String startTime, String endTime, String tag) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		queryFilter.addFilter("partId", "", MatchType.NULL);
		return getAttachmentList(queryFilter);
	}

	/**
	 * 
	 * getCheckAttachments:
	 * 适用: 根据国省检的起点、结束时间、附件标签和partId查询与数据记录相关的附件
	 * @param startTime 开始年份时间 eg：2013
	 * @param endTime 结束年份时间  eg：2017
	 * @param tag 文件标签
	 * @param partId 数据对应的partId
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getCheckAttachments(String startTime,
			String endTime, String tag, String partId) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		queryFilter.addFilter("partId", partId, MatchType.EQ);
		return getAttachmentList(queryFilter);
	}

	/**
	 * 
	 * getCheckAttachments:
	 * 适用: 根据国省检的起点、结束时间、附件标签和partId查询与数据记录相关的附件及所有的规章制度和其他类型文件
	 * @param startTime 开始年份时间 eg：2013
	 * @param endTime 结束年份时间  eg：2017
	 * @param tag 文件标签
	 * @param partId 数据对应的partId
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getCheckAttachmentsByPartId(String startTime,
			String endTime, String tag, String partId) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		queryFilter.addFilterVN("partId", partId);
		return getAttachmentList(queryFilter);
	}

	/**
	 * 
	 * getCheckAttachments:
	 * 适用: 根据国省检的起点、结束时间、附件标签查询与数据记录相关的附件及其所有规章制度和其他类型文件
	 * @param startTime 开始年份时间 eg：2013
	 * @param endTime 结束年份时间  eg：2017
	 * @param tag 文件标签O
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getAllCheckAttachments(String startTime,
			String endTime, String tag) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		return getAttachmentList(queryFilter);
	}
	
	/**
	 * 
	 * getCheckAttachments:
	 * 适用: 根据国省检的附件标签(不筛选时间)查询具体某一类型的文件
	 * @param tag 文件标签O
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getAllCheckAttachments(String tag) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("tag", tag, MatchType.EQ);
		return getAttachmentList(queryFilter);
	}
	
	/**
	 * 
	 * getAllCheckAttachmentsByParTag:
	 * 适用: 根据国省检的附件标签(不筛选时间)查询所有与数据记录相关的附件及其所有规章制度和其他类型文件
	 * @param tag 文件标签O
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getAllCheckAttachmentsByParTag(String tag) throws Exception {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("parentTag", tag, MatchType.EQ);
		return getAttachmentList(queryFilter);
	}
	
	@Override
	public void uploadFile(MultipartFile multipartFile, Attachment attachment) throws Exception {
		String rootPath = ConfigUtils.getConfig("attachment.path"); //获取上传附件的根目录
		// 得到文件后缀名
		final String fileExtension = FileUtils.getTypePart(multipartFile.getOriginalFilename());
		//得到文件的名字
		String fileName = FileUtils.getFileNamePart(multipartFile.getOriginalFilename());
		StringBuilder fileNameBuffer = new StringBuilder();
		fileNameBuffer.append("专项工程").append(Constants.FILE_SEP).append(DateUtils.getCurrentStrDate()).append(Constants.FILE_SEP);
		StringBuilder filePath  = new StringBuilder();
		
		String random = com.pengtu.gsj.utils.StringUtils.getUUID();
		//重新命名文件名（文件名+随机数）
		filePath.append(fileName).append(random).append(".").append(fileExtension);
		fileNameBuffer.append(filePath);
		
		File file = new File(rootPath, fileNameBuffer.toString());
		FileUtils.makeDirectory(file);
		attachment.setFullPath(fileNameBuffer.toString());
		
		/*将文件写入服务器、转换成pdf、生成缩略图均应用多线程来完成，后续完成*/
		multipartFile.transferTo(file); //上传文件到服务器指定位置
		
		attachment.setFileName(FileUtils.getNamePart(multipartFile.getOriginalFilename()));
		attachment.setUpfileFileName(FileUtils.getNamePart(multipartFile.getOriginalFilename()));
		attachment.setSize(ConverPdfUtils.getSize(multipartFile.getSize()));
		attachment.setType(fileExtension);
		attachment.setOrgId(UserUtils.getUser().getOrg().getId());
		attachment.setFileTime(DateUtils.getCurrentDate());
		attachment.setUpfileContentType(multipartFile.getContentType());
		attachment.setUploadPerson(SpringSecurityUtils.getCurrentName());
		attachment.setPath(filePath.toString());
		if (StringUtils.isEmpty(attachment.getYear())) {
			attachment.setYear(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		}
		saveAttachment(attachment);
	}

	/**
	 * partId 专项工程id
	 * 当all为true时   得到专项工程下设施局 所有辖区局的文件
	 * 当all为false时 得到专项工程的设施局文件
	 */
	@Override
	public List<Attachment> getAttachmentsByPartId(String partId, boolean all) {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("partId", partId, MatchType.EQ);
		if (!all) {
			queryFilter.addFilter("tableId", "", MatchType.NULL);
		}
		return attachmentDao.find(queryFilter);
	}

	/**
	 * 得到专项工程下辖区局角色对应的所有附件
	 */
	@Override
	public List<Attachment> getProjectAttachments(String partId, String tableId) {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("partId", partId, MatchType.EQ);
		queryFilter.addFilterVN("tableId", tableId);
		return getAttachmentList(queryFilter);
	}
	
	/**
	 * 
	 * getOrgAndFile:
	 * 适用:根据专项工程id 得到当前用户在当前项目中涉及到的文件
	 * @param projectId 专项工程id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public Map<String, Object> getOrgAndFile(String projectId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(projectId)) {
		List<Attachment> attachments = new ArrayList<Attachment>();
		//若是设施局  能看到所有的附件
		if (UserUtils.hasXqjRole()) { 
			attachments = getProjectAttachments(projectId, UserUtils.getUser().getOrg().getId());
		} else if (UserUtils.hasSsjRole() || UserUtils.hasAdminRole()) { //若是辖区局  能看到当前单位和辖区局的附件
			attachments  = getAttachmentsByPartId(projectId, true);
		} else { 	//若是其他角色  只能看到辖区局传的文件
			attachments = getAttachmentsByPartId(projectId, false);
		}
		List<String> orgs = new ArrayList<String>();
		//得到文件中涉及到的单位id与名称
		for (Attachment attachment : attachments) {
			if (StringUtils.isNotEmpty(attachment.getTableId()) && !orgs.contains(attachment.getTableId())) {
				Organization organization = organizationService.get(attachment.getTableId());
				orgs.add(attachment.getTableId());
				orgs.add(organization.getOrgName());
			}
		}
		map.put("attachments", attachments);	
		map.put("orgs", orgs);
		}
		return map;
	}
	
	/**
	 * 
	 * getCheckAttachmentByTag:
	 * 适用: 根据国省检的起点、结束时间、附件标签查询具体某个类型的文件
	 * @param startTime 开始年份时间 eg：2013
	 * @param endTime 结束年份时间  eg：2017
	 * @param tag 文件标签O
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Attachment> getCheckAttachmentByTag(String startTime, String endTime, String tag) throws Exception{
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("tag", tag, MatchType.EQ);
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("fileTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("fileTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		return getAttachmentList(queryFilter);
	}
	
	/**
	 * 
	 * getCheckYear:
	 * 适用: 根据国省检的起点、结束时间返回时间长度
	 * @param endTime 结束年份时间  eg：2017
	 * @param startTime 开始年份时间 eg：2013
	 * @param temp eg:有些国检表只需要返回四年的数据，结果就是(2017-2013 +1)-1。
	 * @return
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public Integer getCheckYear(String endTime, String startTime, Integer temp){
		if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
			int year = Integer.parseInt(endTime)-Integer.parseInt(startTime)+1;
			if (year <= temp) {
				return 1;
			}
			return year - temp;
		} else {
			return 1;
		}
	}

	@Override
	public List<String> getAttachmentYears() {
		List<String> list = new ArrayList<String>();
		List<Attachment> attachments = attachmentDao.getAttachmentYears();
		if (attachments != null) {
			for (Attachment attachment : attachments) {
				list.add(attachment.getYear());
			}
		}
		return list;
	}
	
	

	/**
	 * 
	 * getCountJson:
	 * 适用:获取某年度各业务模块下所有文件类型的文件数量
	 * @param year 年份信息
	 * @return list集合
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<Map<String, Object>> getCountJson(String year){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		if (StringUtils.isEmpty(year)) {
			return list;
		}
		//得到某年度下所有的日常监管附件
		List<Attachment> attachments = getAttachmentsOfYear(year);
		//得到所有的业务模块
		String parentId = "402880f65b8f1cea015b8f24ae3d0001";
		List<Sysmenu> sysmenus = sysmenuManagerService.getByParentId(parentId);
		for (Sysmenu sysmenu : sysmenus) {
			long gzzd_count = 0;
			long other_count = 0;
			long business_count = 0;
			Iterator<Attachment> iterator = attachments.iterator();
			while (iterator.hasNext()) {
				Attachment attachment = (Attachment) iterator.next();
				if (attachment.getParentTagIds().contains(sysmenu.getId())) {
					if (attachment.getTag().contains("_gzzd")) {
						gzzd_count++;
					} else if (attachment.getTag().contains("_other") || attachment.getTag().contains("_qtwj")) {
						other_count++;
					} else {
						business_count++;
					}
					iterator.remove();
				}
			}
			map = new HashMap<String, Object>();
			map.put("name", sysmenu.getName());
			map.put("group", "总计");
			map.put("value", gzzd_count+other_count+business_count);
			list.add(map);
			map = new HashMap<String, Object>();
			map.put("name", sysmenu.getName());
			map.put("group", "规章制度文件");
			map.put("value", gzzd_count);
			list.add(map);
			map = new HashMap<String, Object>();
			map.put("name", sysmenu.getName());
			map.put("group", "业务类型文件");
			map.put("value", business_count);
			list.add(map);
			map = new HashMap<String, Object>();
			map.put("name", sysmenu.getName());
			map.put("group", "其他文件");
			map.put("value", other_count);
			list.add(map);
		}
		return list;
	}

	@Override
	public List<Attachment> getAttachmentsOfYear(String year) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(year)) {
			queryFilter.addFilter("year", year, MatchType.EQ);
		}
		queryFilter.addFilter("tag", "", MatchType.NOTNULL);
		return attachmentDao.find(queryFilter);
	}

	@Override
	public ResponseEntity<byte[]> exportData(String year) throws Exception {
		if (StringUtils.isEmpty(year)) {
			return null;
		}
		//得到某年度下所有的日常监管附件
		List<Attachment> attachments = getAttachmentsOfYear(year);
		//得到所有的业务模块
		String parentId = "402880f65b8f1cea015b8f24ae3d0001";
		List<Sysmenu> sysmenus = sysmenuManagerService.getByParentId(parentId);
		List<List<String>> list = new ArrayList<List<String>>();
		for (Sysmenu sysmenu : sysmenus) {
			long gzzd_count = 0;
			long other_count = 0;
			long business_count = 0;
			Iterator<Attachment> iterator = attachments.iterator();
			List<String> data = new ArrayList<String>();
			while (iterator.hasNext()) {
				Attachment attachment = (Attachment) iterator.next();
				if (attachment.getParentTagIds().contains(sysmenu.getId())) {
					if (attachment.getTag().contains("_gzzd")) {
						gzzd_count++;
					} else if (attachment.getTag().contains("_other") || attachment.getTag().contains("_qtwj")) {
						other_count++;
					} else {
						business_count++;
					}
					iterator.remove();
				}
			}
			data.add(sysmenu.getName());
			data.add(String.valueOf(gzzd_count+other_count+business_count));
			data.add(String.valueOf(gzzd_count));
			data.add(String.valueOf(business_count));
			data.add(String.valueOf(other_count));
			list.add(data);
		}
		return ExcelImportUtils.insertDataToExcel("attachment.xls", list, "日常监管附件汇总表.xls");
	}
}
